<Project Sdk="Microsoft.DotNet.Helix.Sdk" DefaultTargets="Test">

  <PropertyGroup Condition="'$(AGENT_OS)' == 'Windows_NT'">
    <PerformanceDirectory>%HELIX_WORKITEM_ROOT%\performance</PerformanceDirectory>
    <HelixPreCommands>$(HelixPreCommands) &amp;&amp; robocopy /np /nfl /e %HELIX_CORRELATION_PAYLOAD%\performance $(PerformanceDirectory) /XD %HELIX_CORRELATION_PAYLOAD%\performance\.git</HelixPreCommands>
    <WorkItemCommand>$(PerformanceDirectory)\scripts\benchmarks_ci.py --csproj $(PerformanceDirectory)\$(TargetCsproj)</WorkItemCommand>
    <CliArguments>--dotnet-versions %DOTNET_VERSION% --cli-source-info args --cli-branch %PERFLAB_BRANCH% --cli-commit-sha %PERFLAB_HASH% --cli-repository https://github.com/%PERFLAB_REPO% --cli-source-timestamp %PERFLAB_BUILDTIMESTAMP%</CliArguments>
    <Python>py -3</Python>
    <CoreRun>%HELIX_CORRELATION_PAYLOAD%\Core_Root\CoreRun.exe</CoreRun>
    <BaselineCoreRun>%HELIX_CORRELATION_PAYLOAD%\Baseline_Core_Root\CoreRun.exe</BaselineCoreRun>
    <HelixPreCommands>$(HelixPreCommands);call $(PerformanceDirectory)\tools\machine-setup.cmd;set PYTHONPATH=%HELIX_WORKITEM_PAYLOAD%\scripts%3B%HELIX_WORKITEM_PAYLOAD%</HelixPreCommands>
    <ArtifactsDirectory>%HELIX_WORKITEM_ROOT%\artifacts\BenchmarkDotNet.Artifacts</ArtifactsDirectory>
    <BaselineArtifactsDirectory>%HELIX_WORKITEM_ROOT%\artifacts\BenchmarkDotNet.Artifacts_Baseline</BaselineArtifactsDirectory>
    <ResultsComparer>$(PerformanceDirectory)\src\tools\ResultsComparer\ResultsComparer.csproj</ResultsComparer>
    <DotnetExe>$(PerformanceDirectory)\tools\dotnet\$(Architecture)\dotnet.exe</DotnetExe>
    <Percent>%25%25</Percent>
    <XMLResults>%HELIX_WORKITEM_ROOT%\testResults.xml</XMLResults>
  </PropertyGroup>

  <PropertyGroup Condition="'$(AGENT_OS)' != 'Windows_NT' and '$(RunFromPerfRepo)' == 'false'">
    <BaseDirectory>$HELIX_CORRELATION_PAYLOAD</BaseDirectory>
    <PerformanceDirectory>$HELIX_WORKITEM_ROOT/performance</PerformanceDirectory>
    <HelixPreCommands>$(HelixPreCommands);cp -R $(BaseDirectory)/performance $(PerformanceDirectory)</HelixPreCommands>
  </PropertyGroup>

  <PropertyGroup Condition="'$(AGENT_OS)' != 'Windows_NT' and '$(RunFromPerfRepo)' == 'true'">
    <BaseDirectory>$HELIX_WORKITEM_PAYLOAD</BaseDirectory>
    <PerformanceDirectory>$(BaseDirectory)</PerformanceDirectory>
  </PropertyGroup>

  <PropertyGroup Condition="'$(AGENT_OS)' != 'Windows_NT'">
    <WorkItemCommand>$(PerformanceDirectory)/scripts/benchmarks_ci.py --csproj $(PerformanceDirectory)/$(TargetCsproj)</WorkItemCommand>
    <CliArguments>--dotnet-versions $DOTNET_VERSION --cli-source-info args --cli-branch $PERFLAB_BRANCH --cli-commit-sha $PERFLAB_HASH --cli-repository https://github.com/$PERFLAB_REPO --cli-source-timestamp $PERFLAB_BUILDTIMESTAMP</CliArguments>
    <Python>python3</Python>
    <CoreRun>$(BaseDirectory)/Core_Root/corerun</CoreRun>
    <BaselineCoreRun>$(BaseDirectory)/Baseline_Core_Root/corerun</BaselineCoreRun>
    <HelixPreCommands>$(HelixPreCommands);chmod +x $(PerformanceDirectory)/tools/machine-setup.sh;. $(PerformanceDirectory)/tools/machine-setup.sh</HelixPreCommands>
    <ArtifactsDirectory>$HELIX_WORKITEM_ROOT/artifacts/BenchmarkDotNet.Artifacts</ArtifactsDirectory>
    <BaselineArtifactsDirectory>$HELIX_WORKITEM_ROOT/artifacts/BenchmarkDotNet.Artifacts_Baseline</BaselineArtifactsDirectory>
    <ResultsComparer>$(PerformanceDirectory)/src/tools/ResultsComparer/ResultsComparer.csproj</ResultsComparer>
    <DotnetExe>$(PerformanceDirectory)/tools/dotnet/$(Architecture)/dotnet</DotnetExe>
    <Percent>%25</Percent>
    <XMLResults>$HELIX_WORKITEM_ROOT/testResults.xml</XMLResults>
  </PropertyGroup>

  <PropertyGroup Condition="'$(WasmDotnet)' == 'true'">
    <CliArguments>$(CliArguments) --run-isolated --wasm --dotnet-path %24HELIX_CORRELATION_PAYLOAD/dotnet/</CliArguments>
  </PropertyGroup>

  <PropertyGroup Condition="'$(MonoDotnet)' == 'true' and '$(AGENT_OS)' == 'Windows_NT'">
    <CoreRunArgument>--corerun %HELIX_CORRELATION_PAYLOAD%\dotnet-mono\shared\Microsoft.NETCore.App\$(ProductVersion)\corerun.exe</CoreRunArgument>
  </PropertyGroup>
  <PropertyGroup Condition="'$(MonoDotnet)' == 'true' and '$(AGENT_OS)' != 'Windows_NT'">
    <CoreRunArgument>--corerun $(BaseDirectory)/dotnet-mono/shared/Microsoft.NETCore.App/$(ProductVersion)/corerun</CoreRunArgument>
  </PropertyGroup>

  <PropertyGroup Condition="'$(MonoAOT)' == 'true' and '$(AGENT_OS)' == 'Windows_NT'">
    <HelixPreCommands>$(HelixPreCommands);%HELIX_CORRELATION_PAYLOAD%\monoaot\mono-aot-cross --llvm --version</HelixPreCommands>
  </PropertyGroup>
  <PropertyGroup Condition="'$(MonoAOT)' == 'true' and '$(AGENT_OS)' != 'Windows_NT'">
    <HelixPreCommands>$(HelixPreCommands);$HELIX_CORRELATION_PAYLOAD/monoaot/mono-aot-cross --llvm --version</HelixPreCommands>
  </PropertyGroup>

  <PropertyGroup Condition="'$(UseCoreRun)' == 'true'">
    <CoreRunArgument>--corerun $(CoreRun)</CoreRunArgument>
  </PropertyGroup>

  <PropertyGroup Condition="'$(UseBaselineCoreRun)' == 'true'">
    <BaselineCoreRunArgument>--corerun $(BaselineCoreRun)</BaselineCoreRunArgument>
  </PropertyGroup>

  <PropertyGroup Condition="'$(WorkItemCommand)' != ''">
    <WorkItemCommand>$(Python) $(WorkItemCommand) --incremental no --architecture $(Architecture) -f $(PERFLAB_Framework) $(PerfLabArguments)</WorkItemCommand>
  </PropertyGroup>

  <PropertyGroup Condition="'$(PERFLAB_Framework)' != 'net461'">
    <WorkItemCommand>$(WorkItemCommand) $(CliArguments)</WorkItemCommand>
  </PropertyGroup>

  <PropertyGroup>
    <WorkItemTimeout>6:00</WorkItemTimeout>
    <WorkItemTimeout Condition="'$(OnlySanityCheck)' == 'true'">1:30</WorkItemTimeout>

    <ExtraBenchmarkDotNetArguments Condition="'$(OnlySanityCheck)' == 'true'">$(ExtraBenchmarkDotNetArguments) --filter System.Tests.Perf_*</ExtraBenchmarkDotNetArguments>
  </PropertyGroup>

  <ItemGroup>
    <HelixCorrelationPayload Include="$(CorrelationPayloadDirectory)">
      <PayloadDirectory>%(Identity)</PayloadDirectory>
    </HelixCorrelationPayload>
  </ItemGroup>

  <PropertyGroup>
    <PartitionCount>30</PartitionCount>
  </PropertyGroup>
  <ItemGroup>
    <Partition Include="$(BuildConfig).Partition0" Index="0" />
  </ItemGroup>

  <ItemGroup Condition="'$(OnlySanityCheck)' != 'true'">
    <Partition Include="$(BuildConfig).Partition1" Index="1" />
    <Partition Include="$(BuildConfig).Partition2" Index="2" />
    <Partition Include="$(BuildConfig).Partition3" Index="3" />
    <Partition Include="$(BuildConfig).Partition4" Index="4" />
    <Partition Include="$(BuildConfig).Partition5" Index="5" />
    <Partition Include="$(BuildConfig).Partition6" Index="6" />
    <Partition Include="$(BuildConfig).Partition7" Index="7" />
    <Partition Include="$(BuildConfig).Partition8" Index="8" />
    <Partition Include="$(BuildConfig).Partition9" Index="9" />
    <Partition Include="$(BuildConfig).Partition10" Index="10" />
    <Partition Include="$(BuildConfig).Partition11" Index="11" />
    <Partition Include="$(BuildConfig).Partition12" Index="12" />
    <Partition Include="$(BuildConfig).Partition13" Index="13" />
    <Partition Include="$(BuildConfig).Partition14" Index="14" />
    <Partition Include="$(BuildConfig).Partition15" Index="15" />
    <Partition Include="$(BuildConfig).Partition16" Index="16" />
    <Partition Include="$(BuildConfig).Partition17" Index="17" />
    <Partition Include="$(BuildConfig).Partition18" Index="18" />
    <Partition Include="$(BuildConfig).Partition19" Index="19" />
    <Partition Include="$(BuildConfig).Partition20" Index="20" />
    <Partition Include="$(BuildConfig).Partition21" Index="21" />
    <Partition Include="$(BuildConfig).Partition22" Index="22" />
    <Partition Include="$(BuildConfig).Partition23" Index="23" />
    <Partition Include="$(BuildConfig).Partition24" Index="24" />
    <Partition Include="$(BuildConfig).Partition25" Index="25" />
    <Partition Include="$(BuildConfig).Partition26" Index="26" />
    <Partition Include="$(BuildConfig).Partition27" Index="27" />
    <Partition Include="$(BuildConfig).Partition28" Index="28" />
    <Partition Include="$(BuildConfig).Partition29" Index="29" />
  </ItemGroup>

  <PropertyGroup Condition="'$(Compare)' == 'true'">
    <FailOnTestFailure>false</FailOnTestFailure>
  </PropertyGroup>

  <!--
    Partition the Microbenchmarks project, but nothing else
  -->
  <ItemGroup Condition="$(TargetCsproj.Contains('MicroBenchmarks.csproj'))">
    <HelixWorkItem Include="@(Partition)">
      <PayloadDirectory>$(WorkItemDirectory)</PayloadDirectory>
      <PreCommands Condition="'$(Compare)' == 'true'">$(WorkItemCommand) --bdn-artifacts $(BaselineArtifactsDirectory) --bdn-arguments="--anyCategories $(BDNCategories) $(ExtraBenchmarkDotNetArguments) $(BaselineCoreRunArgument) --partition-count $(PartitionCount) --partition-index %(HelixWorkItem.Index)"</PreCommands>
      <Command Condition="'$(AGENT_OS)' != 'Windows_NT'">
        if [ "x$PERF_PREREQS_INSTALL_FAILED" = "x1" ]; then
          echo "\n\n** Error: Failed to install prerequisites **\n\n"; (exit 1);
        else
          $(WorkItemCommand) --bdn-artifacts $(ArtifactsDirectory) --bdn-arguments="--anyCategories $(BDNCategories) $(ExtraBenchmarkDotNetArguments) $(CoreRunArgument) --partition-count $(PartitionCount) --partition-index %(HelixWorkItem.Index)";
        fi</Command>
      <Command Condition="'$(AGENT_OS)' == 'Windows_NT'">$(WorkItemCommand) --bdn-artifacts $(ArtifactsDirectory) --bdn-arguments="--anyCategories $(BDNCategories) $(ExtraBenchmarkDotNetArguments) $(CoreRunArgument) --partition-count $(PartitionCount) --partition-index %(HelixWorkItem.Index)"</Command>
      <PostCommands Condition="'$(Compare)' == 'true'">$(DotnetExe) run -f $(PERFLAB_Framework) -p $(ResultsComparer) --base $(BaselineArtifactsDirectory) --diff $(ArtifactsDirectory) --threshold 2$(Percent) --xml $(XMLResults);$(FinalCommand)</PostCommands>
      <Timeout>$(WorkItemTimeout)</Timeout>
    </HelixWorkItem>
  </ItemGroup>

  <ItemGroup Condition="!$(TargetCsproj.Contains('MicroBenchmarks.csproj'))">
    <HelixWorkItem Include="$(BuildConfig).WorkItem">
      <PayloadDirectory>$(WorkItemDirectory)</PayloadDirectory>
      <PreCommands Condition="'$(Compare)' == 'true'">$(WorkItemCommand) --bdn-artifacts $(BaselineArtifactsDirectory) --bdn-arguments="--anyCategories $(BDNCategories) $(ExtraBenchmarkDotNetArguments) $(BaselineCoreRunArgument)"</PreCommands>
      <Command Condition="'$(AGENT_OS)' != 'Windows_NT'">
        if [ "x$PERF_PREREQS_INSTALL_FAILED" = "x1" ]; then
          echo "\n\n** Error: Failed to install prerequisites **\n\n"; (exit 1);
        else
          $(WorkItemCommand) --bdn-artifacts $(ArtifactsDirectory) --bdn-arguments="--anyCategories $(BDNCategories) $(ExtraBenchmarkDotNetArguments) $(CoreRunArgument)";
        fi</Command>
      <Command Condition="'$(AGENT_OS)' == 'Windows_NT'">$(WorkItemCommand) --bdn-artifacts $(ArtifactsDirectory) --bdn-arguments="--anyCategories $(BDNCategories) $(ExtraBenchmarkDotNetArguments) $(CoreRunArgument)"</Command>
      <PostCommands Condition="'$(Compare)' == 'true'">$(DotnetExe) run -f $(PERFLAB_Framework) -p $(ResultsComparer) --base $(BaselineArtifactsDirectory) --diff $(ArtifactsDirectory) --threshold 2$(Percent) --xml $(XMLResults)</PostCommands>
      <Timeout>4:00</Timeout>
    </HelixWorkItem>
  </ItemGroup>
</Project>
